package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.domain.Permission;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.PermissionMapper;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
/**
 * 权限管理实现
 */
@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {
    //注入mapper
    @Autowired
    private PermissionMapper permissionMapper;


    //分页查询
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Permission> page =  permissionMapper.selectByCondition(queryString);
        long total = page.getTotal();
        List<Permission> result = page.getResult();
        return new PageResult(total, result);
    }

    //新增权限项
    public void add(Permission permission) {
        permissionMapper.add(permission);
    }

    //根据id查询权限项信息
    public Permission findById(Integer id) {
        return permissionMapper.findById(id);
    }

    //编辑权限项
    public void edit(Permission permission) {
        permissionMapper.edit(permission);
    }

    //根据id删除权限项
    public void delete(Integer id) {
        permissionMapper.deleteRoleIds(id);
        permissionMapper.delete(id);
    }

    //查询所有
    public List<Permission> findAll() {
        return permissionMapper.findAll();
    }

    //根据权限名查询数据库是否该权限已存在
    public Permission findByName(String name) {
        return permissionMapper.findByName(name);
    }

    //根据权限关键字查询数据库是否该权限已存在
    public Permission findByKeyword(String keyword) {
        return permissionMapper.findByKeyword(keyword);
    }

    //根据权限id查询关联数据的数量
    public Integer findCountByPermissionId(Integer permissionId) {
        return permissionMapper.findCountByPermissionId(permissionId);
    }

    //查询所有权限排除自己
    public List<Permission> findAllExOwn(Integer id) {
        return permissionMapper.findAllExOwn(id);
    }
}
